export default (state = { productList: [], shop: [] ,condition:{}}, action) => {
    // console.log(action.foodinfo);

    if (action.type == "showFood") {
        return {
            ...state,
            productList: action.result.data
        }
    } else if (action.type == "ADD") {
        return {
            ...state,
            shop:
                !state.shop.some((item) => (item.id == action.foodinfo.id))
                    ? [...state.shop,
                    {
                        id: action.foodinfo.id,
                        name: action.foodinfo.name,
                        price: action.foodinfo.price,
                        icon: action.foodinfo.icon,
                        quantity: 1
                    }
                    ]
                    : state.shop.map((item) => {
                        if (item.id == action.foodinfo.id) {
                            return {
                                ...item,
                                quantity: item.quantity + 1
                            }
                        }
                        return item;
                    })
        }
    } else if (action.type == "CUT") {
        return {
            ...state,
            shop:
                state.shop.filter((item) => {
                    if (item.id == action.foodinfo.id) {
                        item.quantity-=1;
                        if(item.quantity!=0){
                            return item;
                        }else{
                            return ;
                        }
                    }
                    console.log(item);
                    
                    return item;
                })
        }
    }else if(action.type == "CLEAR"){
        return{
            ...state,
            shop:[]
        }
    }else if(action.type=="createOrder"){
        return{
            ...state,
            shop: [],
            condition:action.result.data
        }
    }

    return state;
}
